--
-- Author: yawei.zhu
-- Date: 2018-08-01 17:40:03
--
-- PvpChampionTeamView
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpChampionTeamScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		executeFunc(self._onCloseView, self)
	end)

	-- 交互界面
	self._ui.Panel:addClickEventListener(function(sender)
		local touchPoint = sender:getTouchEndPosition()
		for i = 1, 3 do
			local character = self._ui["Node_"..i]:getChildByTag(0)
			if character and character:containWorldPoint(touchPoint.x, touchPoint.y) then
				local heroParams = self._heroParams[i]
				if heroParams.uid ~= UD:getUid() then
                	UIHandler:showHeadMenu({type=Enums.MenuType.champion, attr=heroParams, pos = touchPoint})
                end
				break
			end
		end
	end)

	self._ui.tlt:setString( L("champion_team10") )

	self._txtTotalPow = self._ui["Panel/Panel_bom/Text"]
end

function ClassRef:refresh(throneTeam)
	local pow = 0
	self._heroParams = {}

	for i = 1, 3 do
		local heroParams = throneTeam:getUserBaseInfo(i)
		if heroParams then
			self._heroParams[i] = heroParams
			heroParams.inBattle = false
    		heroParams.mountID = 0

    		-- dump(heroParams)
	
    		local AvatarFactory = app:getClass("AvatarFactory")
    		AvatarFactory.preloadHero(heroParams, function()
    		    if tolua.isnull(self) then return end
	
    		    local character = AvatarFactory.createHero(heroParams)
    		    self._ui["Node_"..i]:addChild(character)
    		    character:setTag(0)
    		    character:playAni({name = "mapidle"})
    		    character:setScale(0.9 * 0.85)
    		    -- 自动调整名字的高度
    		    character:addAttachment(self._ui["data_"..i] ,"point", 0, 0)
    		end)

    		local node = self._ui["data_"..i]
    		local name = node:getChildByName("Text_name")
    		-- local txtZy = node:getChildByName("Text_zy")
    		local icon = node:getChildByName("icon")
    		local txtPow = node:getChildByName("num")

    		name:setString(heroParams.name)
    		txtPow:setString(heroParams.combatPower)
			-- txtZy:setString( string.format("<%s>", heroParams:getDuty()) )
			icon:loadTexture( heroParams:getClassIcon() )

			-- 调整 icon 和 战力 的坐标
			display.setNodesMiddle(node, nil, icon, txtPow)

			pow = pow + heroParams.combatPower
		end
	end

	self._txtTotalPow:setString( L("champion_team8") .. pow)
end

return ClassRef
